package com.loe.ktui

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.ViewGroup
import android.widget.RelativeLayout
import androidx.core.content.res.getBooleanOrThrow
import androidx.core.content.res.getDimensionOrThrow
import androidx.core.content.res.getStringOrThrow
import com.loe.ktui.util.KtCornerProvider

open class UiRelativeLayout(id: String? = null) : UiGroup(id)
{
    open fun initUI(context: Context, view: KtRelativeLayout)
    {
        super.initUI(context, view)

        isAlphaClick?.let { view.isAlphaClick = it }

        // 递归子元素
        childs.forEach()
        {
            it.createView(context, view)
        }
    }

    /** 创建view */
    override fun createView(context: Context, parent: ViewGroup?): KtRelativeLayout
    {
        return KtRelativeLayout(context).also()
        {
            initUI(context, it)
            addToParent(parent, it)
        }
    }
}

fun Any.ui_RelativeLayout(id: String? = null, block: (UiRelativeLayout.() -> Unit)? = null): UiRelativeLayout
{
    val ui = UiRelativeLayout(id)
    block?.let { ui.it() }
    if (this is UiGroup) addChild(ui)
    return ui
}

open class KtRelativeLayout : RelativeLayout
{
    fun initUI(id: String? = null, block: (UiRelativeLayout.() -> Unit)? = null)
    {
        removeAllViews()

        val ui = UiRelativeLayout(id)
        block?.let { ui.it() }
        ui.initUI(context, this)
    }

    constructor(context: Context) : super(context)
    {
        initXML()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    {
        initXML(context.obtainStyledAttributes(attrs, R.styleable.KtRelativeLayout))
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    {
        initXML(context.obtainStyledAttributes(attrs, R.styleable.KtRelativeLayout, defStyleAttr, 0))
    }

    private fun initXML(typedArray: TypedArray? = null)
    {
        typedArray?.let()
        {
            if (it.hasValue(R.styleable.KtRelativeLayout_kt_corner))
            {
                val corner = it.getDimensionOrThrow(R.styleable.KtRelativeLayout_kt_corner).toInt()
                val cornerDirect =  KtDirects[it.getInt(R.styleable.KtRelativeLayout_kt_cornerDirect, 4)]

                outlineProvider = KtCornerProvider(corner, cornerDirect, this)
            }
            if (it.hasValue(R.styleable.KtRelativeLayout_kt_isAlphaClick))
            {
                isAlphaClick = it.getBooleanOrThrow(R.styleable.KtRelativeLayout_kt_isAlphaClick)
            }
            if (it.hasValue(R.styleable.KtRelativeLayout_kt_link))
            {
                link = it.getStringOrThrow(R.styleable.KtRelativeLayout_kt_link)
            }

            it.recycle()
        }
    }

    /********************************************** 点击 ****************************************/

    var link = ""
        set(value)
        {
            field = value
            setKtDelayClickListener { KtUi.onLink?.invoke(context, value) }
        }

    var isAlphaClick = false
        set(value)
        {
            field = value
            isClickable = value
        }

    override fun dispatchSetPressed(pressed: Boolean)
    {
        if (isAlphaClick) alpha = if (pressed || !isEnabled) 0.75f else 1f
        super.dispatchSetPressed(pressed)
    }

    override fun setEnabled(enabled: Boolean)
    {
        if (isAlphaClick) alpha = if (enabled) 1f else 0.75f
        super.setEnabled(enabled)
    }
}